package com.example.kiratest.notify.Util;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Map;

public class SmartHttpUtil {

    private static final HttpClient client = HttpClient.newBuilder()
            .version(HttpClient.Version.HTTP_2)
            .connectTimeout(Duration.ofSeconds(10))
            .build();

    public static String get(String url) throws Exception {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .timeout(Duration.ofSeconds(10))
                .GET()
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
        return handleResponse(response);
    }
//
//    public static String post(String url, String json) throws Exception {
//        HttpRequest request = HttpRequest.newBuilder()
//                .uri(URI.create(url))
//                .timeout(Duration.ofSeconds(10))
//                .header("Content-Type", "application/json")
//                .POST(HttpRequest.BodyPublishers.ofString(json))
//                .build();
//
//        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
//        return handleResponse(response);
//    }

    public static String postWithHeader(String url, String json, Map<String,String> header) throws Exception {
        HttpRequest.Builder requestBuilder  = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .timeout(Duration.ofSeconds(10))
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(json));
        // 遍历 header Map，添加所有的 header
        if (header != null) {
            for (Map.Entry<String, String> entry : header.entrySet()) {
                requestBuilder.header(entry.getKey(), entry.getValue());
            }
        }
        HttpResponse<String> response = client.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofString());
        return handleResponse(response);
    }

    private static String handleResponse(HttpResponse<String> response) throws Exception {
        if (response.statusCode() != 200) {
            throw new Exception("HTTP error code: " + response.statusCode() + " - " + response.body());
        }
        return response.body();
    }
//
//
//    public static String getOrganizationByIp(String ip) {
//        String socksHost = "0d54cf087c4c89ec.na.roxlabs.vip"; // SOCKS5 代理地址
//        int socksPort = 4600; // SOCKS5 代理端口
//        String username = "user-phantom001-region-us-sessid-{sessionId}-sesstime-30-keep-true"; // SOCKS5 用户名
//        String password = "qwerasdf1234"; // SOCKS5 密码
//
//        // 创建 SOCKS 代理
//        HttpHost socksProxy = new HttpHost(socksHost, socksPort, "HTTP");
//        Random random = new Random();
//        username = username.replace("{sessionId}", random.nextInt(5,10) + "od");
//        // 创建凭证提供者
//        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(
//                new AuthScope(socksHost, socksPort),
//                new UsernamePasswordCredentials(username, password)
//        );
//
//        // 创建连接管理器
//        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
//        cm.setMaxTotal(100);
//        cm.setDefaultMaxPerRoute(100);
//        cm.setMaxPerRoute(new HttpRoute(socksProxy), 100);
//
//        // 创建 HttpClient
//        try (CloseableHttpClient httpClient = HttpClients.custom()
//                .setConnectionManager(cm)
//                .setDefaultCredentialsProvider(credentialsProvider)
//                .setProxy(socksProxy)
//                .build()) {
//
//            // 构建请求
//            String url = "https://ipinfo.io/" + ip + "/json";
//            HttpGet request = new HttpGet(url);
//
//            // 发送请求并获取响应
//
//            CloseableHttpResponse execute = httpClient.execute(request);
//            String jsonResponse = EntityUtils.toString(execute.getEntity());
//
//            return jsonResponse;
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "";
//        }
//    }

}
